package org.extframework.ld.http.servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.extframework.ld.content.XmlContent;
import org.extframework.ld.http.abstr.AbstractEncryptionWebRequest;
import org.extframework.ld.http.abstr.AbstractEncryptionWebResponse;
import org.extframework.ld.http.abstr.AbstractEncryptionWebServlet;
import org.extframework.ld.util.DateUtil;

import com.sun.jmx.snmp.Timestamp;

public class FileServlet extends AbstractEncryptionWebServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void init(
			AbstractEncryptionWebRequest paramAbstractEncryptionWebRequest,
			AbstractEncryptionWebResponse paramAbstractEncryptionWebResponse)
			throws ServletException, IOException {
		ajaxRequest = false;
	}

	public void index(AbstractEncryptionWebRequest paramHttpServletRequest,
			AbstractEncryptionWebResponse paramHttpServletResponse)
			throws ServletException, IOException {
		
	}

	public void upLoad(AbstractEncryptionWebRequest request,
			AbstractEncryptionWebResponse response) throws Exception {
		
		String webRootPath = getServletConfig().getServletContext().getRealPath("/");
		System.out.println(webRootPath);
		
		response.setContentType("text/html");

		response.setCharacterEncoding("utf-8");
		
		String fileName = null;

		String uploadpath = webRootPath+"\\items\\"; // 用于存放上传文件的目录
		String temppath = webRootPath+"\\items\\temp"; // 用于存放临时文件的目录

		File fpath = new File(uploadpath);
		File fpath1 = new File(temppath);

		// 这个是判断上传的文件夹是否存在，如果不存在就创建一个
		if (fpath.exists()) {
			System.out.println("该文件夹已经存在！");
		} else {
			fpath.mkdirs();
		}
		if (fpath1.exists()) {
			System.out.println("该文件夹已经存在！");
		} else {
			fpath1.mkdirs();
		}

		System.out.println("开始上传文件！");

		DiskFileUpload fu = new DiskFileUpload();
		fu.setHeaderEncoding("utf-8");

		fu.setFileSizeMax(20000000); // 设置最大文件
		fu.setSizeThreshold(4096); // 设置缓冲区大小，4kb
		fu.setRepositoryPath(temppath); // 设置临时文件目录

		List fileItems = fu.parseRequest(request); // 得到所有文件
		Iterator it = fileItems.iterator();
		int filesize = request.getContentLength();
		request.getSession().setAttribute("filesize", filesize);
		// 依次处理每个文件
		while (it.hasNext()) {
			FileItem fi = (FileItem) it.next();
			String fname = fi.getName(); // 获取文件名，包括路径
			if (fname != null) {
				String exfile = fname.substring(fname.indexOf("."));

				// 下面这个是用时间戳产生一个上次文件名，一般在数据库保存的时候，都是保存两个名字的，原来的名字和保存的绝对路径。
				String pfilename = DateUtil.getDateTimeToFileName();
				
				fileName = pfilename + exfile;
				System.out.println(uploadpath + fileName);
				request.getSession().setAttribute("pathall",
						uploadpath + pfilename + exfile);
				fi.write(new File(uploadpath + "" + fileName));
			}
		}
		response.printStr("{success:true,message:{status:'ok',fileName:'"+XmlContent.getRootUrl()+"/items/"+fileName+"'}}");
	}

}
